Imports System.Data.SqlClient
Imports System.Drawing
Imports DevExpress.XtraGrid
Imports DevExpress.XtraGrid.Views.Base
Imports DevExpress.XtraGrid.Columns
Imports DevExpress.XtraEditors
Imports DevExpress.XtraEditors.Repository

'--- Created Date On : 23-Nov-2010 (Jagdish)

Public Class frmRptInventoryActivityReport
    Dim mblnActivate As Boolean = False

    Private Sub frmRptInventoryActivityReport_Activated(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Activated
        If mblnActivate = False Then
            Call PopulateGridLookupDX(lkCategory, "Category", "CategoryId", "tblCategory", "CategoryId,Category", , "Category", True)
            Call PopulateData()
            mblnActivate = True
        End If
    End Sub

    Private Sub frmRptInventoryActivityReport_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
        If e.KeyCode = Keys.F5 Then
            Call PopulateData()
        End If
    End Sub
    Private Sub frmRptInventoryActivityReport_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Call SetImageControl(Me)
        Call GetAccessPermission(Me, "frmRptInventoryActivityReport")
        dtFrom.DateTime = Format(DateAdd(DateInterval.Day, 1, DateAdd(DateInterval.Month, -1, gdtCurrentDate)), gstrDtFormat)
        dtTo.DateTime = Format(gdtCurrentDate, gstrDtFormat)
    End Sub

    Private Sub PopulateData()
        Dim strQuery As String
        Dim dsTemp As DataSet
        Try
            Call DeleteFromTable("tblTempItemStock")
            strQuery = "Select ItemId from tblItem "
            If Val(lkCategory.EditValue) > 0 Then
                strQuery = strQuery & " Where CategoryId=" & lkCategory.EditValue & ""
            End If
            strQuery = strQuery & " Order by ItemId"
            dsTemp = FillDataSet(strQuery)

            If dsTemp.Tables(0).Rows.Count > 0 Then
                For IntCtr As Integer = 0 To dsTemp.Tables(0).Rows.Count - 1
                    Call ExecuteStoreProcedure("sp_ItemStock", SmartReplace(CDate(dtFrom.EditValue)) & " , " & SmartReplace(CDate(dtTo.EditValue)) & " , " & dsTemp.Tables(0).Rows(IntCtr).Item("ItemId"))
                Next
            End If
            dsTemp.Dispose()
            strQuery = "Select Date,ItemName [Item Name],OpStock [Opening Stock],Purchase,Adjustment,QtyIssue [Issue],ClStock [Closing Stock] " & _
                        " from tblTempItemStock INNER JOIN tblItem on tblTempItemStock.ItemId=tblItem.ItemId " & _
                        " where Date Between '" & Format(CDate(dtFrom.EditValue), gstrDtFormat) & "' and '" & Format(CDate(dtTo.EditValue), gstrDtFormat) & "'"
            dsTemp = FillDataSet(strQuery)
            With grdRecords
                .BeginUpdate()
                .DataSource = dsTemp.Tables(0)
                .EndUpdate()
            End With
            With gdvRecords
                .Columns("Item Name").Group()
                .Columns("Date").DisplayFormat.FormatType = DevExpress.Utils.FormatType.DateTime
                .Columns("Date").DisplayFormat.FormatString = gstrDtFormat
                .ExpandAllGroups()
                .OptionsBehavior.Editable = False
                .OptionsView.ColumnAutoWidth = True
                .BestFitColumns()
            End With
        Catch ex As Exception
            MessageBox.Show(ex.Message, gstrMessage, MessageBoxButtons.OK, MessageBoxIcon.Error)
            Call AddErrorLog("Error " & Err.Number & " (" & Err.Description & ") in Procedure " & _
                 Reflection.MethodBase.GetCurrentMethod.Name & " Of " & Me.Name)
        End Try
    End Sub

    Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
        Me.Close()
        GC.Collect()
    End Sub
    Private Sub lkCategory_ButtonClick(ByVal sender As Object, ByVal e As DevExpress.XtraEditors.Controls.ButtonPressedEventArgs) Handles lkCategory.ButtonClick
        If e.Button.Index = 1 Then
            Call PopulateGridLookupDX(lkCategory, "Category", "CategoryId", "tblCategory", "CategoryId,Category", , "Category", True)
        End If
    End Sub

    Private Sub lkCategory_EditValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles lkCategory.EditValueChanged
        If mblnActivate = True Then Call PopulateData()
    End Sub

    Private Sub btnFilter_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFilter.Click
        Call PopulateData()
    End Sub
End Class
